package Day09.main;


import Day09.bean.Express;
import Day09.dao.ExpressDao;
import Day09.test.view.Views;

public class Main {
    private static Views v = new Views();//初始化view对象
    private static ExpressDao dao = new ExpressDao();//初始化dao对象
    public static void main(String[] args) {
        v.welcome();//欢迎
        m:while(true) {
            int menu = v.menu();//菜单
            switch (menu) {
                case 0:
                    break m;
                case 1:
                    cClient();
                    break;
                case 2:
                    uClient();
                    break;
            }
        }
        v.bye();
    }

    private static void cClient() {
        while (true) {
            int menu = v.cMenu();
            switch (menu) {
                case 0:
                    return;
                case 1: {
                    Express e = v.insert();//提示输入快递信息
                    Express e2 = dao.findByNumber(e.getNumber());//此快递是否储存过
                    if(e2 == null){
                        dao.add(e);//存入
                        v.printExpress(e);
                    }else{
                        v.expressExist();//单号已存在
                    }
                    break;
                }
                case 2: {//快递修改
                    String number = v.findByNumber();//提示用户输入快递信息
                    Express e = dao.findByNumber(number);//查找数据
                    Express e2 = e;
                    //打印快递信息
                    if(e2 == null){
                        v.printfNull();
                    }else {
                        v.update(e2);
                        dao.update(e,e2);
                        v.printExpress(e2);
                    }

                    break;
                }
                case 3: {//删除快递
                    String number = v.findByNumber();
                    Express e = dao.findByNumber(number);
                    if(e == null){
                        v.printfNull();
                    }else{
                        v.printExpress(e);
                        int type = v.delete();
                        if(type == 1){
                            dao.delete(e);
                            v.success();
                        }
                    }
                    break;
                }
                case 4: {
                    Express[][] data = dao.findAll();
                    v.printfAll(data);
                    break;
                }
            }
        }
    }

    private static void uClient() {
        int mun = v.uMenu();//取件码获取
        //查找
        Express e = dao.findByCode(mun);
        if(e == null){
            v.printfNull();
        }else{
            v.success();
            v.printExpress(e);
            dao.delete(e);
        }

    }
}
